package com.mage.servlet;

import com.mage.bean.User;
import com.mage.dao.UserDao;
import com.mage.dao.impl.UserDaoImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Servlet implementation class UpdateUserServlet
 */
@WebServlet("/UpdateUserController")
public class UpdateUserController extends HttpServlet {
	private static final long serialVersionUID = 1L;
    public UpdateUserController() {
        super();
    }
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try {
			request.setCharacterEncoding("UTF-8");
			//1. 获取客户端提交上来的数据
			System.out.println(request.getParameter("userid"));
			int userid = Integer.parseInt(request.getParameter("userid"));
			String username = request.getParameter("username");
			String passwords = request.getParameter("password");
			String birthday = request.getParameter("birthday");
			String sex = request.getParameter("sex");
			System.out.println(sex);
			int usertype = Integer.parseInt(request.getParameter("usertype"));
			System.out.println(request.getParameter("money"));
			Double money = Double.parseDouble(request.getParameter("money"));
			Double yxf = Double.parseDouble(request.getParameter("yxf"));
			Double wxf = Double.parseDouble(request.getParameter("wxf"));
			String dengji = request.getParameter("dengji");
			String phone = request.getParameter("phone");

			Date rhtime = new Date();
			//2. 添加到数据库
			//string -- date
			Date date= new SimpleDateFormat("yyyy-MM-dd").parse(birthday);
			User User = new User(userid,username, passwords,date,sex, usertype,money,yxf,wxf,dengji,phone,rhtime);
			UserDao service = new UserDaoImpl();
			service.update(User);
			request.getRequestDispatcher("UserListServlet").forward(request, response);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
